package cl.tinet.finalexam;

import java.io.Serializable;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import cl.tinet.finalexam.ejb.reserves.ReservesBeanInterface;
import cl.tinet.finalexam.exceptions.ReserveCollitionException;
import cl.tinet.finalexam.exceptions.RoomNotFoundException;
import cl.tinet.finalexam.exceptions.UserNotFoundException;
import cl.tinet.finalexam.model.ReserveModel;

@ManagedBean
@ViewScoped
public class ReserveMB implements Serializable {

    /**
     * TODO doc information expected for serialVersionUID attribute.
     */
    private static final long serialVersionUID = 1L;

    @EJB(mappedName = "reservebeanImpl", name = "reservebeanImpl")
    private transient ReservesBeanInterface reserveBean;

    @ManagedProperty(value = "UserMB")
    private UserMB userMBSession;

    private ReserveModel newReserve;

    public ReserveMB() {
        if (newReserve == null) {
            newReserve = new ReserveModel();
        }
    }

    public void addReserve() {
        try {
            reserveBean.addReserve(userMBSession.getUserSession().getUserID(),
                    newReserve);
        } catch (UserNotFoundException e) {
            System.out.println("ERROR - User not found: " + e.getErr());
        } catch (RoomNotFoundException e) {
            System.out.println("ERROR - Room not Found ID: " + e.getErr());
        } catch (ReserveCollitionException e) {
            System.out.println("ERROR - Reserve Collition ID: " + e.getErr());
        }
    }

    /**
     * Allows to get the reserveBean attribute value.
     * 
     * @return the reserveBean value.
     */
    public ReservesBeanInterface getReserveBean() {
        return reserveBean;
    }

    /**
     * Allows to set the reserveBean attribute value.
     * 
     * @param reserveBean
     *            new value for reserveBean attribute.
     */
    public void setReserveBean(ReservesBeanInterface reserveBean) {
        this.reserveBean = reserveBean;
    }

    /**
     * Allows to get the userMBSession attribute value.
     * 
     * @return the userMBSession value.
     */
    public UserMB getUserMBSession() {
        return userMBSession;
    }

    /**
     * Allows to set the userMBSession attribute value.
     * 
     * @param userMBSession
     *            new value for userMBSession attribute.
     */
    public void setUserMBSession(UserMB userMBSession) {
        this.userMBSession = userMBSession;
    }

    /**
     * Allows to get the newReserve attribute value.
     * 
     * @return the newReserve value.
     */
    public ReserveModel getNewReserve() {
        return newReserve;
    }

    /**
     * Allows to set the newReserve attribute value.
     * 
     * @param newReserve
     *            new value for newReserve attribute.
     */
    public void setNewReserve(ReserveModel newReserve) {
        this.newReserve = newReserve;
    }

}
